// pages/ls/ls.js
import WxValidate from './../../utils/validate';
import {
    message,
    session
} from './../../utils/helper';
import {
    sendPhoneCode,
    submitApplicationForEntry
} from './../../services/ls';

//获取应用实例
const app = getApp();
//实例化本地缓存
const cookie = new session();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        flag: false,
        tip: false,
        errorMsg: [],
        disabled: false,
        codeDis: false,
        phoneCode: "获取验证码",
        telephone: "",
        codePhone: "",
        color: '#34CC4D',
        name_value: '',
        mobile_value: '',
        code_value: ''
    },

    onLoad: function() {
        //显示当前页面的转发按钮
        wx.showShareMenu({
            withShareTicket: true
        });
    },

    //发送验证码
    changeCode() {
        let _this = this;
        const params = {
            openid: app.globalData.openid,
            mobile: this.data.telephone
        };
        this.mobileValidate();
        if (!this.WxValidate.checkForm(params)) {
            let errors = this.WxValidate.validationErrors(),
                error = [];
            error[0] = errors[0];
            this.setData({
                errorMsg: error,
                tip: true,
            });
            return
        }
        this.setData({
            codeDis: true
        })
        //发送短信验证码
        sendPhoneCode(params, function(res) {
            if (res.status != "OK") {
                _this.setData({
                    phoneCode: "获取验证码",
                    flag: false,
                    codeDis: false
                })
                message(res.msg);
            } else {
                _this.setData({
                    phoneCode: 60,
                    flag: true,
                    color: '#B2B2B2'
                })
                let time = setInterval(() => {
                    let phoneCode = _this.data.phoneCode
                    phoneCode--
                    _this.setData({
                        phoneCode: phoneCode
                    })
                    if (phoneCode == 0) {
                        clearInterval(time)
                        _this.setData({
                            phoneCode: "获取验证码",
                            flag: false,
                            codeDis: false,
                            color: '#34CC4D'
                        })
                    }
                }, 1000)
            }
        });
    },

    //手机号码输入事件
    phoneinput(e) {
        this.setData({
            telephone: e.detail.value
        })
    },

    //验证码输入事件
    codeinput(e) {
        this.setData({
            codePhone: e.detail.value
        })
    },

    //转发小程序函数
    onShareAppMessage: function(res) {
        if (res.from === 'button') {
            // 来自页面内转发按钮
            console.log(res.target);
        }
        return {
            title: '律师入驻',
            path: 'pages/ls/ls'
        }
    },

    //校验表单函数
    formsubmit: function(e) {
        let params = e.detail.value,
            that = this;
        this.formValidate();
        // 传入表单数据，调用验证方法
        if (this.WxValidate.checkForm(params)) {
            this.setData({
                errorMsg: '',
                tip: false,
                disabled: true
            });
            params.openid = app.globalData.openid;
            submitApplicationForEntry(params, function(res) {
                if (res.status === "OK") {
                    message('提交成功,请保持电话畅通,稍后我们工作人员会与你联系',()=>{
                        const len = getCurrentPages().length;
                        if (len > 1) {
                            wx.navigateBack({
                                delta: 1
                            })
                        }
                    }),
                    that.setData({
                        name_value: '',
                        mobile_value: '',
                        code_value: ''
                    });
                } else {
                    message(res.msg)
                }
                that.setData({
                    disabled: false
                });
            });
        } else {
            let errors = this.WxValidate.validationErrors(),
                error = [];
            error[0] = errors[0];
            this.setData({
                errorMsg: error,
                tip: true,
            });
        }
        return false;
    },

    //整个表单验证规则
    formValidate() {
        // 验证字段的规则
        const rules = {
            name: {
                required: true,
            },
            mobile: {
                required: true,
                tel: true
            },
            code: {
                required: true,
                minlength: 6,
                maxlength: 6
            }
        }
        // 验证字段的提示信息，若不传则调用默认的信息
        const messages = {
            mobile: {
                required: '请输入手机号',
                tel: '请输入正确的手机号',
            },
            name: {
                required: '请输入您的姓名',
            },
            code: {
                required: '请输入验证码',
                minlength: '验证码至少6个字符',
                maxlength: '验证码最多6个字符'
            }
        }
        // 创建实例对象
        this.WxValidate = new WxValidate(rules, messages)
    },

    //手机号验证规则
    mobileValidate() {
        // 验证字段的规则
        const rules = {
            mobile: {
                required: true,
                tel: true
            }
        }
        // 验证字段的提示信息，若不传则调用默认的信息
        const messages = {
            mobile: {
                required: '请输入手机号',
                tel: '请输入正确的手机号',
            }
        }
        // 创建实例对象
        this.WxValidate = new WxValidate(rules, messages)
    }
})